package com.sys.basics.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sys.basics.config.annotation.SysLogAnnotation;
import lombok.extern.slf4j.Slf4j;
import com.sys.basics.config.jsonRes.JsonResult;
import com.sys.basics.config.jsonRes.PageParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sys.basics.entity.SysScheduleJob;
import com.sys.basics.service.SysScheduleJobService;
import io.swagger.annotations.*;
import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;

/**
 * @author: ares
 * @date: 2021-10-29
 * @description: 定时任务表 前端控制器
 */
@Validated
@Slf4j
@Api(tags = "定时任务管理")
@RestController
@RequestMapping("/basics/sysScheduleJob")
public class SysScheduleJobController extends AbstractController<SysScheduleJobService, SysScheduleJob> {

	@Autowired
	SysScheduleJobService sysScheduleJobService;

	@SysLogAnnotation("获取支持的定时任务类型")
	@ApiOperation("获取支持的定时任务类型")
	@GetMapping("/getSupportiveJobTasks")
	public JsonResult<List<Object>> getSupportiveJobTasks() {
		JsonResult<List<Object>> result = new JsonResult<>();
		return result.success("获取支持的定时任务类型成功", sysScheduleJobService.getSupportiveJobTasks());
	}

	@SysLogAnnotation("新增定时任务")
	@Override
	@ApiOperation("新增定时任务")
	public JsonResult insert(@RequestBody @Valid SysScheduleJob sysScheduleJob) {
		if (ObjectUtil.isAllNotEmpty(sysScheduleJob)) {
			boolean rsg = sysScheduleJobService.createJob(sysScheduleJob);
			if (rsg) {
				result.success("新增定时任务成功");
			} else {
				result.error("新增定时任务失败！");
			}
		} else {
			result.error("请传入正确参数！");
		}
		return result;
	}

	@SysLogAnnotation("更新定时任务")
	@Override
	@ApiOperation("更新定时任务")
	public JsonResult update(@RequestBody @Valid SysScheduleJob sysScheduleJob) {
		if (ObjectUtil.isNotEmpty(sysScheduleJob)) {
			SysScheduleJob dbScheduleJob = sysScheduleJobService.getById(sysScheduleJob.getId());
			if (ObjectUtil.isNull(dbScheduleJob)){
				result.error("定时任务不存在！");
			}
			boolean rsg = sysScheduleJobService.updateJob(sysScheduleJob);
			if (rsg) {
				result.success("更新定时任务成功");
			} else {
				result.error("更新定时任务失败！");
			}
		} else {
			result.error("请传入正确参数！");
		}
		return result;
	}

	@SysLogAnnotation("批量执行定时任务")
	@ApiOperation("批量执行")
	@PostMapping("/batchRun")
	public JsonResult batchRun(@RequestBody List<Long> scheduleJobIds){
		JsonResult result = new JsonResult();
		Map<String, Object> batchRunRes = sysScheduleJobService.batchRun(scheduleJobIds);
		Integer successNum = (Integer) batchRunRes.get("successNum");
		List<String> errorMsg = (List) batchRunRes.get("errorMsg");

		if(successNum == 0){
			return result.error("执行失败");
		}else if (successNum > 0 && successNum < scheduleJobIds.size() && errorMsg.size() >0){
			return result.success("批量执行部分成功", StringUtils.join(errorMsg,"|"));
		}else if(successNum == scheduleJobIds.size()){
			return result.success("执行成功");
		}
		return result.error("批量执行异常");
	}

	@SysLogAnnotation("批量暂停定时任务")
	@ApiOperation("批量暂停")
	@PostMapping("/batchPause")
	public JsonResult batchPause(@RequestBody List<Long> scheduleJobIds){
		JsonResult result = new JsonResult();
		Map<String, Object> batchPauseRes = sysScheduleJobService.batchPause(scheduleJobIds);
		Integer successNum = (Integer) batchPauseRes.get("successNum");
		List<String> errorMsg = (List) batchPauseRes.get("errorMsg");

		if(successNum == 0){
			return result.error("暂停失败");
		}else if (successNum > 0 && successNum < scheduleJobIds.size() && errorMsg.size() >0){
			return result.success("批量暂停部分成功", StringUtils.join(errorMsg,"|"));
		}else if(successNum == scheduleJobIds.size()){
			return result.success("暂停成功");
		}
		return result.error("批量暂停异常");
	}

	@SysLogAnnotation("批量恢复定时任务")
	@ApiOperation("批量恢复")
	@PostMapping("/batchResume")
	public JsonResult batchResume(@RequestBody List<Long> scheduleJobIds){
		JsonResult result = new JsonResult();
		Map<String, Object> batchResumeRes = sysScheduleJobService.batchResume(scheduleJobIds);
		Integer successNum = (Integer) batchResumeRes.get("successNum");
		List<String> errorMsg = (List) batchResumeRes.get("errorMsg");

		if(successNum == 0){
			return result.error("恢复失败");
		}else if (successNum > 0 && successNum < scheduleJobIds.size() && errorMsg.size() >0){
			return result.success("批量恢复部分成功", StringUtils.join(errorMsg,"|"));
		}else if(successNum == scheduleJobIds.size()){
			return result.success("恢复成功");
		}
		return result.error("批量恢复异常");
	}

	@SysLogAnnotation("批量删除定时任务")
	@ApiOperation("批量删除")
	@PostMapping("/batchRemove")
	public JsonResult batchRemove(@RequestBody List<Long> scheduleJobIds){
		JsonResult result = new JsonResult();
		Map<String, Object> batchRemoveRes = sysScheduleJobService.batchRemove(scheduleJobIds);
		Integer successNum = (Integer) batchRemoveRes.get("successNum");
		List<String> errorMsg = (List) batchRemoveRes.get("errorMsg");

		if(successNum == 0){
			return result.error("删除失败");
		}else if (successNum > 0 && successNum < scheduleJobIds.size() && errorMsg.size() >0){
			return result.success("批量删除部分成功", StringUtils.join(errorMsg,"|"));
		}else if(successNum == scheduleJobIds.size()){
			return result.success("删除成功");
		}
		return result.error("批量删除异常");
	}
 }
